/**
 * Copyright &copy; 2012-2015 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.soccer.cms.modules.cms.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.soccer.cms.common.config.Global;
import com.soccer.cms.common.persistence.Page;
import com.soccer.cms.common.web.BaseController;
import com.soccer.cms.common.utils.ActionHelp;
import com.soccer.cms.common.utils.StringUtils;
import com.soccer.cms.common.utils.vo.TreeVo;
import com.soccer.cms.modules.cms.entity.Club;
import com.soccer.cms.modules.cms.entity.Equipment;
import com.soccer.cms.modules.cms.service.ClubService;
import com.soccer.cms.modules.cms.service.EquipmentService;

/**
 * 装备Controller
 * @author zouqh
 * @version 2015-05-26
 */
@SuppressWarnings("unused")
@Controller
@RequestMapping(value = "${adminPath}/cms/equipment")
public class EquipmentController extends BaseController {

	@Autowired
	private EquipmentService equipmentService;
	
	@Autowired
	private ClubService clubService;
	
	@ModelAttribute
	public Equipment get(@RequestParam(required=false) String id) {
		Equipment entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = equipmentService.get(id);
		}
		if (entity == null){
			entity = new Equipment();
		}
		return entity;
	}
	
	@RequiresPermissions("cms:equipment:view")
	@RequestMapping(value = {"index"})
	public String indexPage(Equipment equipment, HttpServletRequest request, HttpServletResponse response, Model model) {
		
		return "modules/cms/equipmentIndex";
	}
	
	@RequiresPermissions("cms:equipment:view")
	@RequestMapping(value = {"list", ""})
	public String list(Equipment equipment, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Equipment> page = equipmentService.findPage(new Page<Equipment>(request, response), equipment); 
		model.addAttribute("page", page);
		return "modules/cms/equipmentList";
	}

	@RequiresPermissions("cms:equipment:view")
	@RequestMapping(value = "form")
	public String form(Equipment equipment, Model model) {
		model.addAttribute("equipment", equipment);
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		model.addAttribute("opt",request.getParameter("opt"));
		return "modules/cms/equipmentForm";
	}

	@RequiresPermissions("cms:equipment:edit")
	@RequestMapping(value = "save")
	public String save(Equipment equipment, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, equipment)){
			return form(equipment, model);
		}
		equipmentService.save(equipment);
		//addMessage(redirectAttributes, "保存装备成功");
		//return "redirect:"+Global.getAdminPath()+"/cms/equipment/?repage";
		model.addAttribute("equipment", equipment);
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		model.addAttribute("opt","save");
		model.addAttribute("message","保存装备成功");
		return "modules/cms/equipmentForm";
	}
	
	@RequiresPermissions("cms:equipment:edit")
	@RequestMapping(value = "delete")
	public String delete(Equipment equipment, RedirectAttributes redirectAttributes) {
		equipmentService.delete(equipment);
		addMessage(redirectAttributes, "删除装备成功");
		return "redirect:"+Global.getAdminPath()+"/cms/equipment/?repage";
	}

	@RequiresPermissions("cms:equipment:edit")
	@RequestMapping(value = "batchDelete")
	public String delete(String ids, RedirectAttributes redirectAttributes) {
		equipmentService.delete(ids);
		addMessage(redirectAttributes, "删除装备成功");
		return "redirect:"+Global.getAdminPath()+"/cms/equipment/?repage";
	}
	
	@RequiresPermissions("cms:equipment:view")
	@RequestMapping(value = "asyncTree")
	public void asyncLoadTreeData(String id,HttpServletRequest request, HttpServletResponse response, Model model) {
		
		List<Club> clubs = clubService.findList(new Club());
		List<TreeVo> treeVoList = new ArrayList<TreeVo>();
		
		if(clubs!=null&&clubs.size()>0){
			for (Club club : clubs) {
				TreeVo treeVo = new TreeVo();
				treeVo.setUk(club.getId());
				treeVo.setId(club.getId());
				treeVo.setName(club.getName());
				treeVo.setpId(id);
				treeVo.setInfo(club.getRemarks());
				treeVo.setChecked(false);
				treeVo.setIsParent(false);
				treeVo.setHasChild(false);
				treeVoList.add(treeVo);
			}
		}
		JSONArray jsonArray = JSONArray.fromObject(treeVoList);
		String treeListStr = jsonArray.toString();
		ActionHelp.WriteStrToOut(response, treeListStr);
	}
}